package com.leyou.controller;

import com.leyou.common.vo.BrandDTO;
import com.leyou.common.vo.PageResult;
import com.leyou.service.BrandService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    //    分页
    @GetMapping("page")
    public ResponseEntity<PageResult<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc", defaultValue = "false") Boolean desc
    ) {
        return ResponseEntity.ok(brandService.queryBrandByPage(page, rows, key, sortBy, desc));
    }

    @PostMapping
    public ResponseEntity<Void> addBrand(BrandDTO brandDTO, @RequestParam("cids") List<Long> cids) {

        this.brandService.addBrand(brandDTO, cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();       //返回201
    }

    @PutMapping//http://api.leyou.com/api/item/brand
    public ResponseEntity<Void> updateBrand(
            BrandDTO brandDTO,
            @RequestParam("cids") List<Long> cids
    ) {
        brandService.updateBrand(brandDTO, cids);

//        build(): 构建没有主体的响应实体。
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

//    /api/item/brand/of/category?id=76

    /**
     * 根据分类查询品牌
     *
     * @param categoryId 商品分类id
     * @return 该分类下的品牌的集合
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryByCategoryId(@RequestParam("id") Long categoryId) {
        return ResponseEntity.ok(this.brandService.queryByCategoryId(categoryId));
    }

    //    根据查询品牌对象
    @GetMapping("list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("cids") List<Long> cids) {
        return ResponseEntity.ok(this.brandService.queryBrandByIds(cids));
    }

//    通过spuid查询brandDTO
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id")Long bid){
        return ResponseEntity.ok(this.brandService.queryById(bid));
    }
}
